// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: [],
    backImage: '../../images/whitebg/back2.svg',
    collImag: "whitebg/coll.svg",
    likeImag: "whitebg/like.svg",
    coll: 0,
    like: 0
  },
  showcoll: function () {
    const db = wx.cloud.database()
    const _ = db.command
    db.collection("book_list")
      .where(
        _.and([{
            'collection_info.coll_openid': getApp().globalData.openid
          },
          {
            _id: this.data.options_id.id
          }
        ]))
      .get().then(res => {
        if (res.data.length != 0) {
          this.setData({
            collImag: "on/coll.svg",
            coll: 1
          });
        }
      })
  },

  clickcoll: function () {
    const db = wx.cloud.database()
    const _ = db.command
    if (this.data.coll == 0) {
      db.collection("book_list")
        .where({
          _id: this.data.options_id.id
        })
        .update({
          data: {
            collection_info: _.push({
              each: [{
                coll_openid: getApp().globalData.openid
              }],
            })
          },
          success: function () {
            wx.showToast({
              title: '收藏成功',
              icon: 'success',
              duration: 900,
            })
          }
        })
      this.setData({
        collImag: "on/coll.svg",
        coll: 1
      });
    } else if (this.data.coll == 1) {
      const db = wx.cloud.database()
      const _ = db.command
      db.collection("book_list")
        .where({
          _id: this.data.options_id.id
        })
        .update({
          data: {
            collection_info: _.pull({
              coll_openid: getApp().globalData.openid
            })
          },
          success: function () {
            wx.showToast({
              title: '取消成功',
              icon: 'success',
              duration: 900,
            })
          }
        })
      this.setData({
        collImag: "whitebg/coll.svg",
        coll: 0
      });
    }
  },


  clickLike: function () {
    const db = wx.cloud.database()
    const _ = db.command
    if (this.data.like == 0) {
      db.collection("book_list")
        .where({
          _id: this.data.options_id.id
        })
        .update({
          data: {
            like: _.inc(1)
          },
          success: function () {
            wx.showToast({
              title: '喜欢成功',
              icon: 'success',
              duration: 900,
            })
          }
        })
      this.setData({
        likeImag: "on/like.svg",
        like:1
      });
    }else if(this.data.like == 1){
      db.collection("book_list")
        .where({
          _id: this.data.options_id.id
        })
        .update({
          data: {
            like: _.inc(-1)
          },
          success: function () {
            wx.showToast({
              title: '取消成功',
              icon: 'success',
              duration: 900,
            })
          }
        })
      this.setData({
        likeImag: "whitebg/like.svg",
        like:0
      });
    }
  },

  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 390) {
      this.setData({
        background: this.data.detail.colors[0] + ';-webkit-transition:0.5s;transition: 0.5s;',
      });
    } else {
      this.setData({
        background: 'null' + ';-webkit-transition: 0.5s;transition: 0.5s;'
      });
    }
  },

  norss: function () {
    wx.showToast({
      icon: 'none',
      title: '已被借阅了！',
      duration: 2000
    })
  },
  yesrss: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../contact/contact?id=' + id
    });
  },

  starCount: function (originStars) {
    //计算星星显示需要的数据，用数组stars存储五个值，分别对应每个位置的星星是全星、半星还是空星
    var starNum = originStars * 10 / 10,
      stars = [],
      i = 0;
    do {
      if (starNum >= 1) {
        stars[i] = 'full';
      } else if (starNum >= 0.5) {
        stars[i] = 'half';
      } else {
        stars[i] = 'no';
      }
      starNum--;
      i++;
    } while (i < 5)
    return stars;
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options_id: options,
    })
    this.showcoll()
    const db = wx.cloud.database()
    db.collection("book_list")
      .doc(options.id)
      .get({
        success: res => {
          // substring截取字符
          this.setData({
            detail: res.data,
            pubdate: res.data.pubdate.substring(0, 4),
            stars: this.starCount(res.data.average / 2)
          })
          this.starCount()

          console.log(this.data.detail)
          if (this.data.detail.translator != "") {
            this.setData({
              fanyi: this.data.detail.translator
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad(this.data.options_id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})